package com.algorithms;

import com.algorithms.arrays.Stack;
import com.algorithms.utils.AlgorithmException;

public class test_delimiter_check extends Thread {
	public void run(){
		String a1 = "a[b(c{d*aaa(e)}adf)]asd";
		String a2 = "a[b[c{d*aaa(e)}adf)]asd";
		String a3 = "a[b(dsa*([c{d*aaa(e)}a]df))]asd";

		try{
			System.out.println("check validity of a1: " + 
						checkString(a1));
			System.out.println("check validity of a2: " + 
						checkString(a2));
			System.out.println("check validity of a2: " + 
						checkString(a3));
		}catch(AlgorithmException e){
			e.printStackTrace();
		}
	}
	
	public synchronized boolean checkString(String input) throws AlgorithmException {
		Stack<Character> stack = new Stack<Character>(input.length());
		char c;
		char elmt;
		for (int i = 0; i < input.length(); i++){
			c = input.charAt(i);
			
			switch(c){
				case '{':
				case '[':
				case '(':{
					stack.push(c);
					break;
				}
				case '}':
				case ']':
				case ')':{
					elmt = stack.pop();
					if (c == '}' && elmt != '{' ||
						c == ']' && elmt != '[' ||
						c == ')' && elmt != '('){
						System.err.println(elmt + ":" + c + " -> error! ");
						return false;
					}else{
						System.out.println(elmt + ":" + c);
					}
					break;
				}
			}
		}
		
		return true;
	}
	
	public static void main(String args[]){
		new test_delimiter_check().start();
	}
}
